﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web.Script.Serialization;
using System.Xml;
using UFIDA.U8.DB;                               

namespace UFIDA.U8C.AnLian.NBBankSync
{
    class Common
    {
        //读取App.config的值
        public static string getConfigInfo(string key)
        {
            string value = "";
            value = ConfigurationManager.AppSettings[key];
            return value;
        }

        /// <summary>
        /// 将object对象转换为实体对象
        /// </summary>
        /// <typeparam name="T">实体对象类名</typeparam>
        /// <param name="asObject">object对象</param>
        /// <returns></returns>
        public static T ConvertObjectByJson<T>(object asObject) where T : new()
        {
            var serializer = new JavaScriptSerializer();
            //将object对象转换为json字符
            var json = serializer.Serialize(asObject);
            
            //将json字符转换为实体对象
            var t = serializer.Deserialize<T>(json);
            return t;
        }
        public static T DeserializeObject<T>(string strJson) where T : class
        {
            JavaScriptSerializer js = new JavaScriptSerializer();
            return js.Deserialize<T>(strJson); //反序列化
        }

        public static string GetUrl(string constr, string orgid)
        {
            string sql = "select id,webconfig from wmsconfig with(nolock)";

            DataTable dt = DbHelperSQL.QueryAsDT(sql, constr);
            if (dt!= null && dt.Rows.Count > 0)
            {
                return dt.Rows[0]["webconfig"].ToString();
            }
            else
            {
                return "";
            }
        }

        public static DataSet GetWMSUrl(string constr, string orgid)
        {
            string sql = "select id,webconfig from wmsconfigoff with(nolock)";

            return DbHelperSQL.Query(sql, constr);
        }

        /// <summary>
        /// Json 字符串 转换为 DataTable数据集合
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable ToDataTableTwo(string json)
        {
            DataTable dataTable = new DataTable();  //实例化
            DataTable result;
            try
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
                if (arrayList.Count > 0)
                {
                    foreach (Dictionary<string, object> dictionary in arrayList)
                    {
                        if (dictionary.Keys.Count<string>() == 0)
                        {
                            result = dataTable;
                            return result;
                        }
                        //Columns
                        if (dataTable.Columns.Count == 0)
                        {
                            foreach (string current in dictionary.Keys)
                            {
                                dataTable.Columns.Add(current, dictionary[current] == null  System.Type.GetType("System.String") : dictionary[current].GetType());
                            }
                        }
                        //Rows
                        DataRow dataRow = dataTable.NewRow();
                        foreach (string current in dictionary.Keys)
                        {
                            if (dictionary[current] != null)
                            {
                                dataRow[current] = dictionary[current];
                            }
                        }
                        dataTable.Rows.Add(dataRow); //循环添加行到DataTable中
                    }
                }
            }
            catch(Exception e)
            {
                throw e;
            }
            result = dataTable;
            return result;
        }
        /// <summary>
        /// Json 字符串 转换为 DataTable数据集合
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        public static DataTable ToDataTable(string json)
        {
            DataTable dataTable = new DataTable();  //实例化
            DataTable result;
            try
            {
                JavaScriptSerializer javaScriptSerializer = new JavaScriptSerializer();
                javaScriptSerializer.MaxJsonLength = Int32.MaxValue; //取得最大数值
                ArrayList arrayList = javaScriptSerializer.Deserialize<ArrayList>(json);
                if (arrayList.Count > 0)
                {
                    foreach (Dictionary<string, object> dictionary in arrayList)
                    {
                        if (dictionary.Keys.Count<string>() == 0)
                        {
                            result = dataTable;
                            return result;
                        }
                        //Columns
                        if (dataTable.Columns.Count == 0)
                        {
                            foreach (string current in dictionary.Keys)
                            {
                                if (current != "data")
                                    dataTable.Columns.Add(current, dictionary[current].GetType());
                                else
                                {
                                    ArrayList list = dictionary[current] as ArrayList;
                                    foreach (Dictionary<string, object> dic in list)
                                    {
                                        foreach (string key in dic.Keys)
                                        {
                                            dataTable.Columns.Add(key, dic[key].GetType());
                                        }
                                        break;
                                    }
                                }
                            }
                        }
                        //Rows
                        string root = "";
                        foreach (string current in dictionary.Keys)
                        {
                            if (current != "data")
                                root = current;
                            else
                            {
                                ArrayList list = dictionary[current] as ArrayList;
                                foreach (Dictionary<string, object> dic in list)
                                {
                                    DataRow dataRow = dataTable.NewRow();
                                    dataRow[root] = dictionary[root];
                                    foreach (string key in dic.Keys)
                                    {
                                        dataRow[key] = dic[key];
                                    }
                                    dataTable.Rows.Add(dataRow);
                                }
                            }
                        }
                    }
                }
            }
            catch
            {
            }
            result = dataTable;
            return result;
        }

        /// <summary>
        /// 下载文件到本地
        /// </summary>
        /// <param name="localpath"></param>
        /// <param name="filename"></param>
        /// <param name="url"></param>
        /// <returns></returns>
        public static bool DownLoadFile(string localpath, string filename, string url)
        {
            try
            {
                WebClient wc = new WebClient();
                string cfile = localpath + @"\" + filename;
                if (File.Exists(cfile))
                {
                    File.Delete(cfile);
                }
                if (!Directory.Exists(localpath))
                {
                    Directory.CreateDirectory(localpath);
                }
                wc.DownloadFile(url, cfile+".zip");
                return true;
            }
            catch (Exception ex)
            {
                U8.PUB.LogHelper.WriteLine("DownLoadFile Error:" + ex + "\n");
                throw ex;
            }
        }
        public static DataSet JsonToDataSet(object jsonData)
        {
            try
            {
                XmlDocument xd = new XmlDocument();
                xd = (XmlDocument)JsonConvert.DeserializeXmlNode(jsonData.ToString());
                DataSet ds = new DataSet();
                ds.ReadXml(new XmlNodeReader(xd));
                return ds;
            }
            catch (Exception e)
            {
                throw e;
            }
        }
 
    }
}
